Removing an old, cherished, yet pointless caveat "This documentation is
[supercollider.git] / Help / Streams-Patterns-Events / A Practical Guide / PG_01_Introduction.html
blob4d553f24758183ca369e547d684c67224ef5c45d
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.48">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; min-height: 22.0px}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf}
21 span.s1 {text-decoration: underline ; color: #0000bf}
22 span.s2 {color: #0000bf}
23 span.s3 {text-decoration: underline}
24 span.s4 {color: #000000}
25 span.s5 {color: #0000bf}
26 span.s6 {color: #bf0000}
27 span.Apple-tab-span {white-space:pre}
28 </style>
29 </head>
30 <body>
31 <p class="p1"><b>A Practical Guide to Patterns</b></p>
32 <p class="p2"><b>H. James Harkins</b></p>
33 <p class="p3"><br></p>
34 <p class="p1"><b>Introduction</b></p>
35 <p class="p4"><br></p>
36 <p class="p5">Patterns are one of the most powerful elements of the SuperCollider language, but in some ways they can be difficult to approach using only the class-oriented help files. These documents seek to bridge the gap, explaining the conceptual background behind patterns, describing the usage of specific Pattern classes, and proceeding into examples of practical musical tasks written as patterns.</p>
37 <p class="p4"><br></p>
38 <p class="p4"><br></p>
39 <p class="p1"><b>Contents</b></p>
40 <p class="p4"><br></p>
41 <ul>
42 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_01_Introduction.html"><span class="s1">PG_01_Introduction</span></a>: Fundamental concepts of patterns and streams</li>
43 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_02_Basic_Vocabulary.html"><span class="s1">PG_02_Basic_Vocabulary</span></a>: Common patterns to generate streams of single values</li>
44 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_03_What_Is_Pbind.html"><span class="s1">PG_03_What_Is_Pbind</span></a>: Pattern-based musical sequencing with Pbind and cousins</li>
45 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_04_Words_to_Phrases.html"><span class="s1">PG_04_Words_to_Phrases</span></a>: Nesting patterns, arranging music in terms of phrases</li>
46 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_05_Math_on_Patterns.html"><span class="s1">PG_05_Math_on_Patterns</span></a>: Performing math and collection operations on patterns</li>
47 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_060_Filter_Patterns.html"><span class="s1">PG_06_Filter_Patterns</span></a>: Overview of patterns that modify the behavior of other patterns</li>
48 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_06a_Repetition_Contraint_Patterns.html"><span class="s1">PG_06a_Repetition_Contraint_Patterns</span></a>: Patterns that repeat values, or cut other patterns off early</li>
49 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_06b_Time_Based_Patterns.html"><span class="s1">PG_06b_Time_Based_Patterns</span></a>: Patterns using time as the basis for their evaluation</li>
50 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_06c_Composition_of_Patterns.html"><span class="s1">PG_06c_Composition_of_Patterns</span></a>: Making multiple event patterns act as one</li>
51 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_06d_Parallel_Patterns.html"><span class="s1">PG_06d_Parallel_Patterns</span></a>: Running multiple event patterns simultaneously</li>
52 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_06e_Language_Control.html"><span class="s1">PG_06e_Language_Control</span></a>: Patterns that mimic some language-side control structures</li>
53 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_06f_Server_Control.html"><span class="s1">PG_06f_Server_Control</span></a>: Patterns that manage server-side resources</li>
54 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_06g_Data_Sharing.html"><span class="s1">PG_06g_Data_Sharing</span></a>: Writing patterns to use information from other patterns</li>
55 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_07_Value_Conversions.html"><span class="s1">PG_07_Value_Conversions</span></a>: Describes the default event's conversions for pitch, rhythm and amplitude</li>
56 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">&nbsp;<a href="PG_08_Event_Types_and_Parameters.html"><span class="s2">PG_08_Event_Types_and_Parameters</span></a>: Describes the event types defined in the default event, and the parameters they expect.</li>
57 </ul>
58 <p class="p4"><br></p>
59 <p class="p5">The <b>pattern cookbook</b> is a set of examples with explanations.</p>
60 <p class="p4"><br></p>
61 <ul>
62 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf"> <a href="PG_Cookbook01_Basic_Sequencing.html"><span class="s3">PG_Cookbook01_Basic_Sequencing</span></a></li>
63 <ul>
64 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Playing a predefined note sequence</li>
65 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">“Multichannel” expansion</li>
66 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Using custom SynthDefs (including unpitched SynthDefs)</li>
67 </ul>
68 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf"> <a href="PG_Cookbook02_Manipulating_Patterns.html"><span class="s3">PG_Cookbook02_Manipulating_Patterns</span></a></li>
69 <ul>
70 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Merging (interleaving) independent streams</li>
71 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Reading an array forward or backward arbitrarily</li>
72 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Changing Pbind value patterns on the fly</li>
73 </ul>
74 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf"> <a href="PG_Cookbook03_External_Control.html"><span class="s3">PG_Cookbook03_External_Control</span></a></li>
75 <ul>
76 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Control of parameters by MIDI or HID</li>
77 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Triggering patterns by external control</li>
78 </ul>
79 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_Cookbook04_Sending_MIDI.html"><span class="s1">PG_Cookbook04_Sending_MIDI</span></a>: Sending notes under pattern control to MIDI devices.</li>
80 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf"> <a href="PG_Cookbook05_Using_Samples.html"><span class="s3">PG_Cookbook05_Using_Samples</span></a></li>
81 <ul>
82 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Playing a pattern in time with a sampled loop</li>
83 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Using audio samples to play pitched material</li>
84 </ul>
85 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf"> <a href="PG_Cookbook06_Phrase_Network.html"><span class="s3">PG_Cookbook06_Phrase_Network</span></a></li>
86 <ul>
87 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Building a more complicated melody using shorter phrase patterns</li>
88 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Also illustrates PmonoArtic for portamento with articulation</li>
89 </ul>
90 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf"> <a href="PG_Cookbook07_Rhythmic_Variations.html"><span class="s3">PG_Cookbook07_Rhythmic_Variations</span></a><span class="s4">: An ever-changing drumbeat</span></li>
91 </ul>
92 <p class="p4"><br></p>
93 <p class="p5"><b>Reference material</b></p>
94 <p class="p4"><br></p>
95 <ul>
96 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"> <a href="PG_Ref01_Pattern_Internals.html"><span class="s1">PG_Ref01_Pattern_Internals</span></a>: Details of pattern implementation, with guidance on writing new pattern classes</li>
97 </ul>
98 <p class="p4"><br></p>
99 <p class="p4"><br></p>
100 <p class="p1"><b>Why patterns?</b></p>
101 <p class="p4"><br></p>
102 <p class="p5">Patterns describe calculations without explicitly stating every step. They are a higher-level representation of a computational task. While patterns are not ideally suited for every type of calculation, when they are appropriate they free the user from worrying about every detail of the process. Using patterns, one writes <i>what</i> is supposed to happen, rather than <i>how</i> to accomplish it.</p>
103 <p class="p4"><br></p>
104 <p class="p5">In SuperCollider, patterns are best for tasks that need to produce sequences, or streams, of information. Often these are numbers, but they don't have to be -- patterns can generate any kind of object.</p>
105 <p class="p4"><br></p>
106 <p class="p5">For a simple example, let's count upward starting from 0. We don't know how high we will need to count; we just know that every time we ask for values, we should get a continually increasing series.</p>
107 <p class="p4"><br></p>
108 <p class="p5">Writing everything out, it looks like this. <a href="../../Core/Kernel/Routine.html"><span class="s5">Routine</span></a> is used because this is a control structure that can interrupt what it's doing and remember where it was, so that it can pick up again at exactly that point. You can get some numbers out of it, and call it again later and it will keep counting from the last number returned. (This is an example of a <a href="../Stream.html"><span class="s5">Stream</span></a>. You can find more about Streams in <a href="../Streams-Patterns-Events1.html"><span class="s5">Streams-Patterns-Events1</span></a>.)</p>
109 <p class="p4"><br></p>
110 <p class="p7"><span class="s4">a = </span>Routine<span class="s4"> {</span></p>
111 <p class="p8"><span class="Apple-tab-span"> </span><span class="s5">var</span><span class="Apple-tab-span"> </span>i = 0;</p>
112 <p class="p8"><span class="Apple-tab-span"> </span>loop {</p>
113 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>i.yield;</p>
114 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>i = i + 1;</p>
115 <p class="p8"><span class="Apple-tab-span"> </span>};</p>
116 <p class="p8">};</p>
117 <p class="p9"><br></p>
118 <p class="p8">a.nextN(10);</p>
119 <p class="p4"><br></p>
120 <p class="p5">SuperCollider's built-in control structures allow some simplification.</p>
121 <p class="p4"><br></p>
122 <p class="p7"><span class="s4">a = </span>Routine<span class="s4"> {</span></p>
123 <p class="p8"><span class="Apple-tab-span"> </span>(0..).do { <span class="s5">|i|</span></p>
124 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>i.yield;</p>
125 <p class="p8"><span class="Apple-tab-span"> </span>};</p>
126 <p class="p8">};</p>
127 <p class="p9"><br></p>
128 <p class="p8">a.nextN(10);</p>
129 <p class="p4"><br></p>
130 <p class="p5">But wouldn't it be nice just to say, "Give me an infinite series of numbers starting with 0, increasing by 1"? With <a href="../Patterns/Pseries.html"><span class="s5">Pseries</span></a>, you can. (Here, keyword addressing of the arguments is used for clarity, but 'start', 'step' and 'length' can be omitted.)</p>
131 <p class="p4"><br></p>
132 <p class="p8">a = <span class="s5">Pseries</span>(start: 0, step: 1, length: <span class="s5">inf</span>).asStream;</p>
133 <p class="p9"><br></p>
134 <p class="p8">a.nextN(10);</p>
135 <p class="p4"><br></p>
136 <p class="p5">What are the advantages of the pattern representation?</p>
137 <p class="p4"><br></p>
138 <ul>
139 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">It's shorter.</li>
140 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">It's tested and it works. You don't have to debug how Pseries works (whereas, if you write a Routine, you might make a mistake and then have to find it.)</li>
141 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">With the Routine -- especially if it's complicated -- you will have to decipher it when you come back to the code later. The Pattern states the purpose right there in the code.</li>
142 </ul>
143 <p class="p4"><br></p>
144 <p class="p5">What are some disadvantages?</p>
145 <p class="p4"><br></p>
146 <ul>
147 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Patterns are a new vocabulary to learn. Until you know a critical mass of them, it can be hard to trust them. That's the purpose of this guide!</li>
148 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">If there isn't a pattern that does quite what you want, then it might take some ingenuity to combine patterns into new designs. (Custom behaviors can always be written using Proutine.)</li>
149 </ul>
150 <p class="p4"><br></p>
151 <p class="p5">Using patterns for sequencing might seem to be an advanced usage, but for many uses they are easier than the equivalent code written out step by step. They can serve as a bridge for new and advanced users alike, to represent a musical conception more directly with less connective tissue explicitly stated.</p>
152 <p class="p4"><br></p>
153 <p class="p5">The first step in learning a new language is vocabulary, so the next chapter will concentrate on foundational patterns to generate data streams of nearly every sort.</p>
154 <p class="p4"><br></p>
155 <p class="p4"><br></p>
156 <p class="p2"><b>Patterns versus Streams</b></p>
157 <p class="p4"><br></p>
158 <p class="p5">Some context that is important to keep in mind throughout this discussion is the difference between patterns and streams. In the most general terms:</p>
159 <p class="p4"><br></p>
160 <p class="p5"><i>Patterns define behavior; streams execute it.</i></p>
161 <p class="p4"><br></p>
162 <p class="p5">A pattern is like a blueprint for a building, showing how all the parts fit together. The building doesn't exist until the contractors go and do what the plans specify. When a stream is made from a pattern, it follows the plans laid out in the pattern's blueprint. Rendering the plans into a real-world result does not change the blueprint in any way, but to get the result, the stream has to go through different states.</p>
163 <p class="p4"><br></p>
164 <p class="p5">A pattern is supposed to describe behavior, and in general, evaluating the pattern (by way of a stream) should not change anything in the Pattern object itself. In computer science terms, patterns are <i>stateless</i>; their definition does not change over time. The stream is what keeps track of where we are in the pattern's evaluation.</p>
165 <p class="p4"><br></p>
166 <p class="p5">This explains an easy "gotcha" with patterns -- forgetting to turn the pattern into a stream doesn't get the expected result. Since a pattern doesn't have any concept of a current state, calling 'next' on it is meaningless, so 'next' does what it does for most objects: return the receiver object itself. The method 'asStream' creates the stream conforming to the pattern's specification, and calling 'next' on the stream advances to its next state and returns the new value.</p>
167 <p class="p4"><br></p>
168 <p class="p8">p = <span class="s5">Pseries</span>(0, 1, 10);</p>
169 <p class="p10"><span class="s4">p.next;<span class="Apple-tab-span"> </span></span>// always returns the Pseries, not actual numbers</p>
170 <p class="p9"><br></p>
171 <p class="p8">q = p.asStream;</p>
172 <p class="p10"><span class="s4">q.next;<span class="Apple-tab-span"> </span></span>// calling this repeatedly gets the desired increasing integers</p>
173 <p class="p4"><br></p>
174 <p class="p5">There is a concrete benefit to this strict division of labor. Since the stream does not modify the original pattern, any number of streams can be made from the same blueprint. All of those streams maintain their own independent states, and they can operate concurrently without interfering with each other.</p>
175 <p class="p4"><br></p>
176 <p class="p8">r = p.asStream;</p>
177 <p class="p10"><span class="s4">r.next;<span class="Apple-tab-span"> </span></span>// starts from zero, even though q already gave out some numbers</p>
178 <p class="p9"><br></p>
179 <p class="p10"><span class="s4">q.next;<span class="Apple-tab-span"> </span></span>// resumes where q left off, with no effect from getting values from r</p>
180 <p class="p9"><br></p>
181 <p class="p8">[q.next, r.next]<span class="Apple-tab-span"> </span><span class="s6">// and so on...</span></p>
182 <p class="p4"><br></p>
183 <p class="p5">Bear these points in mind as we move to the next subject: getting basic types of data (deterministic and random) out of patterns.</p>
184 <p class="p4"><br></p>
185 <p class="p4"><br></p>
186 <p class="p11"><span class="s4">Next:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><a href="PG_02_Basic_Vocabulary.html"><span class="s3">PG_02_Basic_Vocabulary</span></a></span></p>
187 <p class="p4"><br></p>
188 <p class="p4"><br></p>
189 <p class="p2"><b>Documentation licensing</b></p>
190 <p class="p4"><br></p>
191 <p class="p5">The initial version of these documents was written December-February 2009 by H. James Harkins. As part of the SuperCollider package, they are released under the Creative Commons CC-BY-SA license. As SuperCollider is an open source project, it is expected (and encouraged) that other users will contribute to the series. Dr. Harkins, however, wishes to retain exclusive rights to revise and republish the original body of work independently of the open-source copy. This excludes material contributed into svn by others. The work may be redistributed at no charge <i>with proper attribution</i>:</p>
192 <p class="p4"><br></p>
193 <p class="p5">Harkins, Henry James. "A Practical Guide to Patterns." <i>SuperCollider 3.3 Documentation</i>, 2009.</p>
194 </body>
195 </html>